package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用相关接口")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;


    /**
     * 文件上传接口
     *
     * @param file
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传接口")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传接口：{}", file);

//        获取原始文件名
        String originalFileName = file.getOriginalFilename();
//        获取后缀
        String extName = originalFileName.substring(originalFileName.lastIndexOf("."));
//        设置文件名 = uuid + 原始文件名后缀
        String fileName = UUID.randomUUID().toString().replace("-", "") + extName;
        String path = aliOssUtil.upload(file.getBytes(), fileName);
//        返回数据
        return Result.success(path);
    }
}
